# ---
# R SCRIPT FOR: Reconsidering Three Puzzles of Chinese Popular Nationalism
# ---

library(tidyverse)
library(bruceR)
library(sjmisc) 
library(sjlabelled)


csgs2015 <- import("csgs2015.sav")

reg_data2015 <- select(
  csgs2015,
  H1A, H1B, H1C, H2, H5, F19B,
  SE1, F11, H14, 
  A1, A3, A4, SE9, SE5, IR9, SE2, B4, B15, F19A, F19C, F19D, F19E, F19F, F19G, F19H, F19I, F19J, F19K,
  province_code, w
)


reg_data2015$n1 <-
  rec(reg_data2015$H1A,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2015$n2 <-
  rec(reg_data2015$H1B,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2015$n3 <-
  rec(reg_data2015$H1C,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2015$n4 <-
  rec(reg_data2015$H2,
      rec = "1=2[very proud]; 2=1[somewhat proud]; 3=-1[not very proud]; 4=-2[not proud at all]; 8:9=0[neutral]",
      as.num = T
  )

reg_data2015$n5 <-
  rec(reg_data2015$H5,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2015$n6 <-
  rec(reg_data2015$F19B,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )


reg_data2015$education <- rec(reg_data2015$SE1,
                         rec = "0,1=1[Elementary school or less];
                            2,3=2[Junior high school];
                            4,5=3[High school];
                            6,7=4[University / College];
                            8=5[Postgraduate];
                            9=NA",
                         as.num = F
)


reg_data2015$demo_values <- rec(reg_data2015$F11,
                         rec = "1,3=1[Substantive democratic values];
                           2,4=2[Procedural democratic values];
                           7,8,9=3[Ambiguous democratic values]",
                          as.num = F
)


reg_data2015$int_threat <- rec(reg_data2015$H14,
                         rec = "1=-3[very positive];2=-2[positive];3=-1[somewhat positive];
                           4=1[somewhat negative];5=2[negative];6=3[very negative];
                           7:9=0",
                         as.num = T
)

reg_data2015$gender <- rec(reg_data2015$A1,
                        rec = "1=1[male]; 2=0[female]; 9=NA",
                        as.num = F
)

reg_data2015$A3 <- rec(reg_data2015$A3, rec="9999=NA;else=copy",as.num = T)
reg_data2015$age <- 2015-reg_data2015$A3

reg_data2015$pol_affiliation <- rec(reg_data2015$SE9,
                         rec = "1,2,4=0[nonmember];3=1[CPC member]; 9=NA;",
                         as.num = F
)

reg_data2015$income <- rec(reg_data2015$SE5,
                           rec = "1 = 1[high];
                             2 = 0[medium];
                             3 = -1[low];
                             4 = -2[very low];
                             8:9 = NA",
                           as.num = T
)

reg_data2015$residency <- rec(reg_data2015$IR9,
                         rec = "1:3=1[urban];
                          4=0[rural];
                          9=NA",
                         as.num = F
)

reg_data2015$ethnicity <- rec(reg_data2015$A4,
                        rec = "1=1[Han]; 2=0[ethnic minority]; 8:9=NA",
                        as.num = F
)

reg_data2015$students <- rec(reg_data2015$SE2,
                         rec = "91=1[student];1:10,92:96=0[nonstudent];98:99=NA",
                         as.num = F
)

reg_data2015$netizens <- rec(reg_data2015$B4,
                        rec = "0:7=1[netizen];8=0[non-netizen];99=NA",
                        as.num = F
)

reg_data2015$econ_performance <- rec(reg_data2015$B15,
                         rec = "1=2[very good];
                      2=1[good];
                      3=0[not good nor bad];
                      4=-1[bad];
                      5=-2[very bad];
                      8:9=NA",
                         as.num = T
)

reg_data2015$a1 <- rec(reg_data2015$F19A,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a2 <- rec(reg_data2015$F19C,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a3 <- rec(reg_data2015$F19D,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a4 <- rec(reg_data2015$F19E,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a5 <- rec(reg_data2015$F19F,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a6 <- rec(reg_data2015$F19G,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a7 <- rec(reg_data2015$F19H,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a8 <- rec(reg_data2015$F19I,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a9 <- rec(reg_data2015$F19J,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015$a10 <- rec(reg_data2015$F19K,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2015 <- reg_data2015 %>%
  rename(weight = w)

csgs2019 <- import("csgs2019.sav")

reg_data2019 <- select(
  csgs2019,
  H1A, H1B, H1C, H2, H5, H5A, F20B,
  SE1, F11, H15, A1, A4, SE13, SE7, IR9, A5, SE2A, B2, B15, F20A, F20C, F20D, F20E, F20F, F20G, F20H, F20I, F20J, F20K,
  province_code, weight_cn
)

reg_data2019$n1 <-
  rec(reg_data2019$H1A,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2019$n2 <-
  rec(reg_data2019$H1B,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2019$n3 <-
  rec(reg_data2019$H1C,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )

reg_data2019$n4 <-
  rec(reg_data2019$H2,
      rec = "1=2[very proud]; 2=1[somewhat proud]; 3=-1[not very proud]; 4=-2[not proud at all]; 8:9=0[neutral]",
      as.num = T
  )

reg_data2019$n5 <- NA
reg_data2019$n5[reg_data2019$H5 == 2 & reg_data2019$H5A == 1] <- -2 
reg_data2019$n5[reg_data2019$H5 == 2 & reg_data2019$H5A == 2] <- -1
reg_data2019$n5[reg_data2019$H5 == 1 & reg_data2019$H5A == 1] <- 2 
reg_data2019$n5[reg_data2019$H5 == 1 & reg_data2019$H5A == 2] <- 1 
reg_data2019$n5[reg_data2019$H5 %in% c(8, 9)] <- 0 
reg_data2019$n5[reg_data2019$H5A %in% c(7, 8, 9)] <- 0 

reg_data2019$n6 <-
  rec(reg_data2019$F20B,
      rec = "1=2[strongly agree]; 2=1[agree]; 3=-1[disagree]; 4=-2[strongly disagree]; 7:9=0[neutral]",
      as.num = T
  )


reg_data2019$education <- rec(reg_data2019$SE1,
                         rec = "0,1,2,3=1[Elementary school or less];
                            4,5=2[Junior high school];
                            6,7=3[High school];
                            8,9=4[University / College];
                            10=5[Postgraduate];
                            99=NA",
                         as.num = F
)


reg_data2019$demo_values <- rec(reg_data2019$F11,
                         rec = "1,3=1[Substantive democratic values];
                           2,4=2[Procedural democratic values];
                           7,8,9=3[Ambiguous democratic values]",
                         as.num = F
)


reg_data2019$int_threat <- rec(reg_data2019$H15,
                         rec = "1=-3[very positive];2=-2[positive];3=-1[somewhat positive];
                           4=1[somewhat negative];5=2[negative];6=3[very negative];
                           7:9=0",
                         as.num = T
)

reg_data2019$gender <- rec(reg_data2019$A1,
                        rec = "1=1[male]; 2=0[female]; 9=NA",
                        as.num = F
)

reg_data2019$A4r <- rec(reg_data2019$A4,
                        rec = "9999=NA;else=copy",
                        as.num = T)
reg_data2019$age <- 2019 - reg_data2019$A4r

reg_data2019$pol_affiliation <- rec(reg_data2019$SE13,
                          rec = "3=1[CPC member]; 1,2,4=0[nonmember]; 9=NA",
                          as.num = F
)

reg_data2019$income <- rec(reg_data2019$SE7,
                           rec = "1 = 2[very high];
                             2 = 1[high];
                             3 = 0[medium];
                             4 = -1[low];
                             5 = -2[very low];
                             7:9 = NA",
                           as.num = T
)

reg_data2019$residency <- rec(reg_data2019$IR9,
                         rec = "1:3=1[urban];
                          4=0[rural];
                          9=NA",
                         as.num = F
)

reg_data2019$ethnicity <- rec(reg_data2019$A5,
                        rec = "1=1[Han]; 2=0[ethnic minority]; 8,9=NA",
                        as.num = F
)


reg_data2019$students <- rec(reg_data2019$SE2A,
                             rec = "9999=NA;1102=1[student];else=0[nonstudent]",
                             as.num = F )

reg_data2019$netizens <- rec(reg_data2019$B2,
                        rec = "1:8=1[netizen];9=0[non-netizen];97:99=NA",
                        as.num = F
)

reg_data2019$econ_performance <- rec(reg_data2019$B15,
                         rec = "1=2[very good];
                      2=1[good];
                      3=0[not good nor bad];
                      4=-1[bad];
                      5=-2[very bad];
                      8:9=NA",
                         as.num = T
)

reg_data2019$a1 <- rec(reg_data2019$F20A,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a2 <- rec(reg_data2019$F20C,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a3 <- rec(reg_data2019$F20D,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a4 <- rec(reg_data2019$F20E,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a5 <- rec(reg_data2019$F20F,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a6 <- rec(reg_data2019$F20G,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a7 <- rec(reg_data2019$F20H,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a8 <- rec(reg_data2019$F20I,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a9 <- rec(reg_data2019$F20J,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019$a10 <- rec(reg_data2019$F20K,
                          rec = "1=2[strongly agree];
                            2=1[agree];
                            3=-1[disagree];
                            4=-2[strongly disagree];
                            7:9=0[neutral]"
)

reg_data2019 <- reg_data2019 %>%
  rename(weight = weight_cn)

reg_data2015$year <- 2015
reg_data2019$year <- 2019
reg_data <- rbind(select(reg_data2015, year, n1:n6, education, demo_values, int_threat, gender, age, pol_affiliation, income, residency, ethnicity, students, netizens, econ_performance, a1:a10, province_code, weight),
                  select(reg_data2019, year, n1:n6, education, demo_values, int_threat, gender, age, pol_affiliation, income, residency, ethnicity, students, netizens, econ_performance, a1:a10, province_code, weight)
)

attach(reg_data)
tmp <- cbind(n1, n2, n3, n4, n5, n6)
detach(reg_data)

reg_data <- mutate(reg_data,
                       nationalism = n1 + n2 + n3 + n4 + n5 + n6
)

attach(reg_data)
tmp <- cbind(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
detach(reg_data)

reg_data <- mutate(reg_data,
                       auth_values = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10
)


write_stata(reg_data, "reg_data.dta")
